---
title: "Figures for Main Text"
output: 
  pdf_document:
    toc: true
urlcolor: blue
header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
---

```{r setup, echo = F, message = F, warning = F, collapse = T}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(knitr)
library(kableExtra)
library(stargazer)
library(sandwich)
library(Zelig)
library(gridExtra)
library(patchwork)
library(Cairo)

# load data 
# set working directory here:
data <- readRDS("main_sample.rds")

# function to compute/display cluster-robust SEs
clSE <- function(model){
  cov <- vcovCL(model, cluster = ~ResponseID)
  robust.se <- sqrt(diag(cov))
  return(robust.se)
}
# function to compute/display new F statistic
F_stat <- function(model){
  vcov <- vcovCL(model, cluster = ~ResponseID)
  F_val <- lmtest::waldtest(model, vcov = vcov)[2,3]
  p_val <- lmtest::waldtest(model, vcov = vcov)[2,4]
  stars <- case_when(p_val < 0.001 ~ "***",
                   p_val >= 0.001 & p_val < 0.01 ~ "**",
                   p_val >= 0.01 & p_val < 0.05 ~ "*",
                   TRUE ~ "")
  df_diff <- (lmtest::waldtest(model, vcov = vcov)[2,2])*-1
  df <- lmtest::waldtest(model, vcov = vcov)[1,1] 
  out<- paste(round(F_val, 3), stars, " (df = ", df_diff, "; ", df, ")", sep = "")
  return(out)
}

# make ResponseID a factor
data$ResponseID <- as.factor(data$ResponseID)
```

\newpage 

# Expected Ratings for Main Effects (no controls, pooled across respondent race, clustered SEs)

The models are:  

1. rating ~ composition + largest_group (Model 1)  

2. rating ~ composition + omitted_group (Model 2)

```{r, collapse = T, message = F, echo = F, warning = F}
# Expected ratings
# Specify models

lg_out <- zelig(rating~ composition + largest_group,
                     id = "ResponseID",
                     model = "normal.gee",
                     data = data,
                     cite = F)

omit_out <- zelig(rating~ composition + omitted_group,
                     id = "ResponseID",
                     model = "normal.gee",
                     data = data,
                     cite = F)

# Set inputs (20 combinations for each model)
## largest group
out_comp50_largest_white <- setx(lg_out,
                                composition = "50/48/2",
                                largest_group = "White")
out_comp50_largest_asian <- setx(lg_out,
                                composition = "50/48/2",
                                largest_group = "Asian")
out_comp50_largest_black <- setx(lg_out,
                                composition = "50/48/2",
                                largest_group = "Black")
out_comp50_largest_latino <- setx(lg_out,
                                composition = "50/48/2",
                                largest_group = "Latino")
out_comp60_largest_white <- setx(lg_out,
                                composition = "60/38/2",
                                largest_group = "White")
out_comp60_largest_asian <- setx(lg_out,
                                composition = "60/38/2",
                                largest_group = "Asian")
out_comp60_largest_black <- setx(lg_out,
                                composition = "60/38/2",
                                largest_group = "Black")
out_comp60_largest_latino <- setx(lg_out,
                                composition = "60/38/2",
                                largest_group = "Latino")
out_comp70_largest_white <- setx(lg_out,
                                composition = "70/28/2",
                                largest_group = "White")
out_comp70_largest_asian <- setx(lg_out,
                                composition = "70/28/2",
                                largest_group = "Asian")
out_comp70_largest_black <- setx(lg_out,
                                composition = "70/28/2",
                                largest_group = "Black")
out_comp70_largest_latino <- setx(lg_out,
                                composition = "70/28/2",
                                largest_group = "Latino")
out_comp80_largest_white <- setx(lg_out,
                                composition = "80/18/2",
                                largest_group = "White")
out_comp80_largest_asian <- setx(lg_out,
                                composition = "80/18/2",
                                largest_group = "Asian")
out_comp80_largest_black <- setx(lg_out,
                                composition = "80/18/2",
                                largest_group = "Black")
out_comp80_largest_latino <- setx(lg_out,
                                composition = "80/18/2",
                                largest_group = "Latino")
out_comp90_largest_white <- setx(lg_out,
                                composition = "90/8/2",
                                largest_group = "White")
out_comp90_largest_asian <- setx(lg_out,
                                composition = "90/8/2",
                                largest_group = "Asian")
out_comp90_largest_black <- setx(lg_out,
                                composition = "90/8/2",
                                largest_group = "Black")
out_comp90_largest_latino <- setx(lg_out,
                                composition = "90/8/2",
                                largest_group = "Latino")

# omitted
out_comp50_omitted_white <- setx(omit_out,
                                composition = "50/48/2",
                                omitted_group = "White")
out_comp50_omitted_asian <- setx(omit_out,
                                composition = "50/48/2",
                                omitted_group = "Asian")
out_comp50_omitted_black <- setx(omit_out,
                                composition = "50/48/2",
                                omitted_group = "Black")
out_comp50_omitted_latino <- setx(omit_out,
                                composition = "50/48/2",
                                omitted_group = "Latino")
out_comp60_omitted_white <- setx(omit_out,
                                composition = "60/38/2",
                                omitted_group = "White")
out_comp60_omitted_asian <- setx(omit_out,
                                composition = "60/38/2",
                                omitted_group = "Asian")
out_comp60_omitted_black <- setx(omit_out,
                                composition = "60/38/2",
                                omitted_group = "Black")
out_comp60_omitted_latino <- setx(omit_out,
                                composition = "60/38/2",
                                omitted_group = "Latino")
out_comp70_omitted_white <- setx(omit_out,
                                composition = "70/28/2",
                                omitted_group = "White")
out_comp70_omitted_asian <- setx(omit_out,
                                composition = "70/28/2",
                                omitted_group = "Asian")
out_comp70_omitted_black <- setx(omit_out,
                                composition = "70/28/2",
                                omitted_group = "Black")
out_comp70_omitted_latino <- setx(omit_out,
                                composition = "70/28/2",
                                omitted_group = "Latino")
out_comp80_omitted_white <- setx(omit_out,
                                composition = "80/18/2",
                                omitted_group = "White")
out_comp80_omitted_asian <- setx(omit_out,
                                composition = "80/18/2",
                                omitted_group = "Asian")
out_comp80_omitted_black <- setx(omit_out,
                                composition = "80/18/2",
                                omitted_group = "Black")
out_comp80_omitted_latino <- setx(omit_out,
                                composition = "80/18/2",
                                omitted_group = "Latino")
out_comp90_omitted_white <- setx(omit_out,
                                composition = "90/8/2",
                                omitted_group = "White")
out_comp90_omitted_asian <- setx(omit_out,
                                composition = "90/8/2",
                                omitted_group = "Asian")
out_comp90_omitted_black <- setx(omit_out,
                                composition = "90/8/2",
                                omitted_group = "Black")
out_comp90_omitted_latino <- setx(omit_out,
                                composition = "90/8/2",
                                omitted_group = "Latino")


# write function to save mean and 95% coverage bounds
set.seed(08540)
save_95evs <- function(model, x_values){
  # simulate 10000 times 
  simulations <- sim(model, x = x_values, num = 10000)
  # save the 10000 expected values
  evs <- unlist(simulations[["sim.out"]][["x"]][["ev"]])
  # save the mean
  ev_mean <- mean(evs)
  # save the lower bound - the 250th lowest value of the string of expected values
  ev_lower <- sort(evs)[250]
  # save the upper bound - the 9750th lowest value of the string of evs
  ev_upper <- sort(evs)[9750]
  # store as vector
  output <- c(ev_mean, ev_lower, ev_upper)
  return(output)
}

######### largest group############################# 
# run function on all 20 combos, save as dataframe
lg_evs <- as.data.frame(rbind(
                       save_95evs(lg_out, out_comp50_largest_white),
                       save_95evs(lg_out, out_comp50_largest_asian),
                       save_95evs(lg_out, out_comp50_largest_black),
                       save_95evs(lg_out, out_comp50_largest_latino),
                       save_95evs(lg_out, out_comp60_largest_white),
                       save_95evs(lg_out, out_comp60_largest_asian),
                       save_95evs(lg_out, out_comp60_largest_black),
                       save_95evs(lg_out, out_comp60_largest_latino),
                       save_95evs(lg_out, out_comp70_largest_white),
                       save_95evs(lg_out, out_comp70_largest_asian),
                       save_95evs(lg_out, out_comp70_largest_black),
                       save_95evs(lg_out, out_comp70_largest_latino),
                       save_95evs(lg_out, out_comp80_largest_white),
                       save_95evs(lg_out, out_comp80_largest_asian),
                       save_95evs(lg_out, out_comp80_largest_black),
                       save_95evs(lg_out, out_comp80_largest_latino),
                       save_95evs(lg_out, out_comp90_largest_white),
                       save_95evs(lg_out, out_comp90_largest_asian),
                       save_95evs(lg_out, out_comp90_largest_black),
                       save_95evs(lg_out, out_comp90_largest_latino)
                       ))
# rename col names
colnames(lg_evs) <- c("Mean", "Lower", "Upper")
# append covariate labels
lg_evs <- lg_evs %>% mutate(Composition = c(rep("50/48/2", 4),
                                            rep("60/38/2", 4),
                                            rep("70/28/2", 4),
                                            rep("80/18/2", 4),
                                            rep("90/8/2", 4)),
                       LargestGroup = rep(c("Whites", "Asians", "Blacks", "Latinos"),5))

lg_evs$LargestGroup <- as.factor(lg_evs$LargestGroup)
lg_evs$LargestGroup <- fct_relevel(lg_evs$LargestGroup,
                                 c("Whites",
                                   "Latinos",
                                   "Asians",
                                   "Blacks"))

########## omitted group ####################################
# run function on all 20 combos, save as dataframe
omit_evs <- as.data.frame(rbind(
                       save_95evs(omit_out, out_comp50_omitted_white),
                       save_95evs(omit_out, out_comp50_omitted_asian),
                       save_95evs(omit_out, out_comp50_omitted_black),
                       save_95evs(omit_out, out_comp50_omitted_latino),
                       save_95evs(omit_out, out_comp60_omitted_white),
                       save_95evs(omit_out, out_comp60_omitted_asian),
                       save_95evs(omit_out, out_comp60_omitted_black),
                       save_95evs(omit_out, out_comp60_omitted_latino),
                       save_95evs(omit_out, out_comp70_omitted_white),
                       save_95evs(omit_out, out_comp70_omitted_asian),
                       save_95evs(omit_out, out_comp70_omitted_black),
                       save_95evs(omit_out, out_comp70_omitted_latino),
                       save_95evs(omit_out, out_comp80_omitted_white),
                       save_95evs(omit_out, out_comp80_omitted_asian),
                       save_95evs(omit_out, out_comp80_omitted_black),
                       save_95evs(omit_out, out_comp80_omitted_latino),
                       save_95evs(omit_out, out_comp90_omitted_white),
                       save_95evs(omit_out, out_comp90_omitted_asian),
                       save_95evs(omit_out, out_comp90_omitted_black),
                       save_95evs(omit_out, out_comp90_omitted_latino)
                       ))
# rename col names
colnames(omit_evs) <- c("Mean", "Lower", "Upper")
# append covariate labels
omit_evs <- omit_evs %>% mutate(Composition = c(rep("50/48/2", 4),
                                            rep("60/38/2", 4),
                                            rep("70/28/2", 4),
                                            rep("80/18/2", 4),
                                            rep("90/8/2", 4)),
                       OmittedGroup = rep(c("Whites", "Asians", "Blacks", "Latinos"),5))

omit_evs$OmittedGroup <- as.factor(omit_evs$OmittedGroup)
omit_evs$OmittedGroup <- fct_relevel(omit_evs$OmittedGroup,
                                 c("Whites",
                                   "Latinos",
                                   "Asians",
                                   "Blacks"))
```


```{r, fig.align= "center", fig.height = 3.54, fig.width= 4.72, echo = F, warning = F, message = F}
# plot largest group
fig1a <- ggplot(lg_evs,
                aes(x = fct_rev(Composition), y = Mean, group = LargestGroup)) +
  geom_point(aes(color = LargestGroup), 
             stat = "summary", fun.y = sum,
             size = 1,
             position = position_dodge(-0.3)) +
  geom_errorbar(aes(ymin = Lower, ymax = Upper, 
                    color = LargestGroup),
                width = 0.2,
                position = position_dodge(-0.3)) +
  stat_summary(fun.y = sum, geom = "line",
               aes(color = LargestGroup),
               position = position_dodge(-0.3)) +
  theme_bw() +
  ylim(2, 5) +
  labs(y = "Perceived Diversity", x = "Neighborhood Distribution \n (Heterogeneity \U2192)", 
       color = "Largest Group") +
  theme(legend.position="bottom", 
        axis.text=element_text(size=6),
        axis.title=element_text(size=7),
        legend.text=element_text(size=6),
        legend.title=element_text(size=7),
        legend.margin = margin(t = -0.05, unit='cm'),
        legend.key.height = unit(0.2, "cm")) +
  guides(color = guide_legend(title.position="top", 
                              title.hjust = 0.5, 
                              nrow = 2,
                              byrow = TRUE))


# plot omitted group
fig1b <- ggplot(omit_evs,
       aes(x = fct_rev(Composition), y = Mean, group = OmittedGroup)) +
  geom_point(aes(color = OmittedGroup), 
             stat = "summary", fun.y = sum,
             size = 1,
             position = position_dodge(-0.3)) +
  geom_errorbar(aes(ymin = Lower, ymax = Upper, 
                    color = OmittedGroup),
                width = 0.2,
                position = position_dodge(-0.3)) +
  stat_summary(fun.y = sum, geom = "line",
               aes(color = OmittedGroup),
               position = position_dodge(-0.3)) +
  theme_bw() +
  ylim(2, 5) +
  labs(x = "Neighborhood Distribution \n (Heterogeneity \U2192)", y = "",
       color = "Absent Group") +
  theme(legend.position="bottom", 
        axis.text=element_text(size=6),
        axis.title=element_text(size=7),
        legend.text=element_text(size=6),
        legend.title=element_text(size=7),
        legend.margin = margin(t = -0.05, unit='cm'),
        legend.key.height = unit(0.2, "cm")) +
  guides(color = guide_legend(title.position="top", 
                              title.hjust = 0.5,
                              nrow = 2,
                              byrow = TRUE))

fig1 <- fig1a + fig1b 

fig1

ggsave("fig1.pdf", fig1, width = 11, height = 10, units = "cm", dpi = 600, device=cairo_pdf)

```

\newpage

# Expected Ratings from 2-way Interaction Models

The model is: rating ~ composition x race + largest_group x race + controls 

Median and modal values of control valuables (within-race): 

```{r, collapse = T, echo = F, results = 'as.is', message = F, warning = F}
getmode <- function(v) {
   uniqv <- unique(v)
   uniqv[which.max(tabulate(match(v, uniqv)))]
}

controls_table <- data %>% group_by(race) %>% 
  summarise(Age = median(age),
            Gender = getmode(gender),
            Education = getmode(educ),
            Income = median(inc_num),
            Place_of_Birth = getmode(born))

```


```{r, collapse = T, message = F, echo = F}
# Expected ratings
# Specify model

ratings_out <- zelig(rating~ composition*race + largest_group*race +
                       age + gender + educ + inc_num + born,
                     id = "ResponseID",
                     model = "normal.gee",
                     data = data,
                     cite = F)

# Set inputs (80 combinations in all)
## white Rs
out_comp50_White_Rwhite <- setx(ratings_out,
                                composition = "50/48/2",
                                largest_group = "White",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp50_Asian_Rwhite <- setx(ratings_out,
                                composition = "50/48/2",
                                largest_group = "Asian",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp50_Black_Rwhite <- setx(ratings_out,
                                composition = "50/48/2",
                                largest_group = "Black",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp50_Latino_Rwhite <- setx(ratings_out,
                                composition = "50/48/2",
                                largest_group = "Latino",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp60_White_Rwhite <- setx(ratings_out,
                                composition = "60/38/2",
                                largest_group = "White",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp60_Asian_Rwhite <- setx(ratings_out,
                                composition = "60/38/2",
                                largest_group = "Asian",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp60_Black_Rwhite <- setx(ratings_out,
                                composition = "60/38/2",
                                largest_group = "Black",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp60_Latino_Rwhite <- setx(ratings_out,
                                composition = "60/38/2",
                                largest_group = "Latino",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp70_White_Rwhite <- setx(ratings_out,
                                composition = "70/28/2",
                                largest_group = "White",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp70_Asian_Rwhite <- setx(ratings_out,
                                composition = "70/28/2",
                                largest_group = "Asian",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp70_Black_Rwhite <- setx(ratings_out,
                                composition = "70/28/2",
                                largest_group = "Black",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp70_Latino_Rwhite <- setx(ratings_out,
                                composition = "70/28/2",
                                largest_group = "Latino",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp80_White_Rwhite <- setx(ratings_out,
                                composition = "80/18/2",
                                largest_group = "White",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp80_Asian_Rwhite <- setx(ratings_out,
                                composition = "80/18/2",
                                largest_group = "Asian",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp80_Black_Rwhite <- setx(ratings_out,
                                composition = "80/18/2",
                                largest_group = "Black",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp80_Latino_Rwhite <- setx(ratings_out,
                                composition = "80/18/2",
                                largest_group = "Latino",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp90_White_Rwhite <- setx(ratings_out,
                                composition = "90/8/2",
                                largest_group = "White",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp90_Asian_Rwhite <- setx(ratings_out,
                                composition = "90/8/2",
                                largest_group = "Asian",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp90_Black_Rwhite <- setx(ratings_out,
                                composition = "90/8/2",
                                largest_group = "Black",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
out_comp90_Latino_Rwhite <- setx(ratings_out,
                                composition = "90/8/2",
                                largest_group = "Latino",
                                race = "White",
                                age = 51 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 75, 
                                born = "United States")
## black Rs
out_comp50_White_Rblack <- setx(ratings_out,
                                composition = "50/48/2",
                                largest_group = "White",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp50_Asian_Rblack <- setx(ratings_out,
                                composition = "50/48/2",
                                largest_group = "Asian",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp50_Black_Rblack <- setx(ratings_out,
                                composition = "50/48/2",
                                largest_group = "Black",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp50_Latino_Rblack <- setx(ratings_out,
                                composition = "50/48/2",
                                largest_group = "Latino",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp60_White_Rblack <- setx(ratings_out,
                                composition = "60/38/2",
                                largest_group = "White",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp60_Asian_Rblack <- setx(ratings_out,
                                composition = "60/38/2",
                                largest_group = "Asian",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp60_Black_Rblack <- setx(ratings_out,
                                composition = "60/38/2",
                                largest_group = "Black",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp60_Latino_Rblack <- setx(ratings_out,
                                composition = "60/38/2",
                                largest_group = "Latino",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp70_White_Rblack <- setx(ratings_out,
                                composition = "70/28/2",
                                largest_group = "White",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp70_Asian_Rblack <- setx(ratings_out,
                                composition = "70/28/2",
                                largest_group = "Asian",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp70_Black_Rblack <- setx(ratings_out,
                                composition = "70/28/2",
                                largest_group = "Black",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp70_Latino_Rblack <- setx(ratings_out,
                                composition = "70/28/2",
                                largest_group = "Latino",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp80_White_Rblack <- setx(ratings_out,
                                composition = "80/18/2",
                                largest_group = "White",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp80_Asian_Rblack <- setx(ratings_out,
                                composition = "80/18/2",
                                largest_group = "Asian",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp80_Black_Rblack <- setx(ratings_out,
                                composition = "80/18/2",
                                largest_group = "Black",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp80_Latino_Rblack <- setx(ratings_out,
                                composition = "80/18/2",
                                largest_group = "Latino",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp90_White_Rblack <- setx(ratings_out,
                                composition = "90/8/2",
                                largest_group = "White",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp90_Asian_Rblack <- setx(ratings_out,
                                composition = "90/8/2",
                                largest_group = "Asian",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp90_Black_Rblack <- setx(ratings_out,
                                composition = "90/8/2",
                                largest_group = "Black",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
out_comp90_Latino_Rblack <- setx(ratings_out,
                                composition = "90/8/2",
                                largest_group = "Latino",
                                race = "Black/African American",
                                age = 43 ,
                                gender = "Female",
                                educ = "High school/GED",
                                inc_num = 50, 
                                born = "United States")
## Hispanic Rs
out_comp50_White_Rhisp <- setx(ratings_out,
                                composition = "50/48/2",
                                largest_group = "White",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp50_Asian_Rhisp <- setx(ratings_out,
                                composition = "50/48/2",
                                largest_group = "Asian",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp50_Black_Rhisp <- setx(ratings_out,
                                composition = "50/48/2",
                                largest_group = "Black",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp50_Latino_Rhisp <- setx(ratings_out,
                                composition = "50/48/2",
                                largest_group = "Latino",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp60_White_Rhisp <- setx(ratings_out,
                                composition = "60/38/2",
                                largest_group = "White",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp60_Asian_Rhisp <- setx(ratings_out,
                                composition = "60/38/2",
                                largest_group = "Asian",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp60_Black_Rhisp <- setx(ratings_out,
                                composition = "60/38/2",
                                largest_group = "Black",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp60_Latino_Rhisp <- setx(ratings_out,
                                composition = "60/38/2",
                                largest_group = "Latino",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp70_White_Rhisp <- setx(ratings_out,
                                composition = "70/28/2",
                                largest_group = "White",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp70_Asian_Rhisp <- setx(ratings_out,
                                composition = "70/28/2",
                                largest_group = "Asian",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp70_Black_Rhisp <- setx(ratings_out,
                                composition = "70/28/2",
                                largest_group = "Black",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp70_Latino_Rhisp <- setx(ratings_out,
                                composition = "70/28/2",
                                largest_group = "Latino",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp80_White_Rhisp <- setx(ratings_out,
                                composition = "80/18/2",
                                largest_group = "White",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp80_Asian_Rhisp <- setx(ratings_out,
                                composition = "80/18/2",
                                largest_group = "Asian",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp80_Black_Rhisp <- setx(ratings_out,
                                composition = "80/18/2",
                                largest_group = "Black",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp80_Latino_Rhisp <- setx(ratings_out,
                                composition = "80/18/2",
                                largest_group = "Latino",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp90_White_Rhisp <- setx(ratings_out,
                                composition = "90/8/2",
                                largest_group = "White",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp90_Asian_Rhisp <- setx(ratings_out,
                                composition = "90/8/2",
                                largest_group = "Asian",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp90_Black_Rhisp <- setx(ratings_out,
                                composition = "90/8/2",
                                largest_group = "Black",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
out_comp90_Latino_Rhisp <- setx(ratings_out,
                                composition = "90/8/2",
                                largest_group = "Latino",
                                race = "Hispanic/Latino(a)",
                                age = 40 ,
                                gender = "Female",
                                educ = "Some college",
                                inc_num = 65, 
                                born = "United States")
## Asian Rs
out_comp50_White_Rasian <- setx(ratings_out,
                                composition = "50/48/2",
                                largest_group = "White",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp50_Asian_Rasian <- setx(ratings_out,
                                composition = "50/48/2",
                                largest_group = "Asian",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp50_Black_Rasian <- setx(ratings_out,
                                composition = "50/48/2",
                                largest_group = "Black",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp50_Latino_Rasian <- setx(ratings_out,
                                composition = "50/48/2",
                                largest_group = "Latino",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp60_White_Rasian <- setx(ratings_out,
                                composition = "60/38/2",
                                largest_group = "White",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp60_Asian_Rasian <- setx(ratings_out,
                                composition = "60/38/2",
                                largest_group = "Asian",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp60_Black_Rasian <- setx(ratings_out,
                                composition = "60/38/2",
                                largest_group = "Black",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp60_Latino_Rasian <- setx(ratings_out,
                                composition = "60/38/2",
                                largest_group = "Latino",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp70_White_Rasian <- setx(ratings_out,
                                composition = "70/28/2",
                                largest_group = "White",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp70_Asian_Rasian <- setx(ratings_out,
                                composition = "70/28/2",
                                largest_group = "Asian",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp70_Black_Rasian <- setx(ratings_out,
                                composition = "70/28/2",
                                largest_group = "Black",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp70_Latino_Rasian <- setx(ratings_out,
                                composition = "70/28/2",
                                largest_group = "Latino",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp80_White_Rasian <- setx(ratings_out,
                                composition = "80/18/2",
                                largest_group = "White",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp80_Asian_Rasian <- setx(ratings_out,
                                composition = "80/18/2",
                                largest_group = "Asian",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp80_Black_Rasian <- setx(ratings_out,
                                composition = "80/18/2",
                                largest_group = "Black",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp80_Latino_Rasian <- setx(ratings_out,
                                composition = "80/18/2",
                                largest_group = "Latino",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp90_White_Rasian <- setx(ratings_out,
                                composition = "90/8/2",
                                largest_group = "White",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp90_Asian_Rasian <- setx(ratings_out,
                                composition = "90/8/2",
                                largest_group = "Asian",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp90_Black_Rasian <- setx(ratings_out,
                                composition = "90/8/2",
                                largest_group = "Black",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")
out_comp90_Latino_Rasian <- setx(ratings_out,
                                composition = "90/8/2",
                                largest_group = "Latino",
                                race = "Asian/Asian American",
                                age = 43 ,
                                gender = "Female",
                                educ = "4-year degree (Bachelor's)",
                                inc_num = 75, 
                                born = "United States")

# run function on all 80 combos, save as dataframe
ratings_evs <- as.data.frame(rbind(
                       save_95evs(ratings_out, out_comp50_White_Rwhite),
                       save_95evs(ratings_out, out_comp50_Asian_Rwhite),
                       save_95evs(ratings_out, out_comp50_Black_Rwhite),
                       save_95evs(ratings_out, out_comp50_Latino_Rwhite),
                       save_95evs(ratings_out, out_comp60_White_Rwhite),
                       save_95evs(ratings_out, out_comp60_Asian_Rwhite),
                       save_95evs(ratings_out, out_comp60_Black_Rwhite),
                       save_95evs(ratings_out, out_comp60_Latino_Rwhite),
                       save_95evs(ratings_out, out_comp70_White_Rwhite),
                       save_95evs(ratings_out, out_comp70_Asian_Rwhite),
                       save_95evs(ratings_out, out_comp70_Black_Rwhite),
                       save_95evs(ratings_out, out_comp70_Latino_Rwhite),
                       save_95evs(ratings_out, out_comp80_White_Rwhite),
                       save_95evs(ratings_out, out_comp80_Asian_Rwhite),
                       save_95evs(ratings_out, out_comp80_Black_Rwhite),
                       save_95evs(ratings_out, out_comp80_Latino_Rwhite),
                       save_95evs(ratings_out, out_comp90_White_Rwhite),
                       save_95evs(ratings_out, out_comp90_Asian_Rwhite),
                       save_95evs(ratings_out, out_comp90_Black_Rwhite),
                       save_95evs(ratings_out, out_comp90_Latino_Rwhite),
                       save_95evs(ratings_out, out_comp50_White_Rblack),
                       save_95evs(ratings_out, out_comp50_Asian_Rblack),
                       save_95evs(ratings_out, out_comp50_Black_Rblack),
                       save_95evs(ratings_out, out_comp50_Latino_Rblack),
                       save_95evs(ratings_out, out_comp60_White_Rblack),
                       save_95evs(ratings_out, out_comp60_Asian_Rblack),
                       save_95evs(ratings_out, out_comp60_Black_Rblack),
                       save_95evs(ratings_out, out_comp60_Latino_Rblack),
                       save_95evs(ratings_out, out_comp70_White_Rblack),
                       save_95evs(ratings_out, out_comp70_Asian_Rblack),
                       save_95evs(ratings_out, out_comp70_Black_Rblack),
                       save_95evs(ratings_out, out_comp70_Latino_Rblack),
                       save_95evs(ratings_out, out_comp80_White_Rblack),
                       save_95evs(ratings_out, out_comp80_Asian_Rblack),
                       save_95evs(ratings_out, out_comp80_Black_Rblack),
                       save_95evs(ratings_out, out_comp80_Latino_Rblack),
                       save_95evs(ratings_out, out_comp90_White_Rblack),
                       save_95evs(ratings_out, out_comp90_Asian_Rblack),
                       save_95evs(ratings_out, out_comp90_Black_Rblack),
                       save_95evs(ratings_out, out_comp90_Latino_Rblack),
                       save_95evs(ratings_out, out_comp50_White_Rhisp),
                       save_95evs(ratings_out, out_comp50_Asian_Rhisp),
                       save_95evs(ratings_out, out_comp50_Black_Rhisp),
                       save_95evs(ratings_out, out_comp50_Latino_Rhisp),
                       save_95evs(ratings_out, out_comp60_White_Rhisp),
                       save_95evs(ratings_out, out_comp60_Asian_Rhisp),
                       save_95evs(ratings_out, out_comp60_Black_Rhisp),
                       save_95evs(ratings_out, out_comp60_Latino_Rhisp),
                       save_95evs(ratings_out, out_comp70_White_Rhisp),
                       save_95evs(ratings_out, out_comp70_Asian_Rhisp),
                       save_95evs(ratings_out, out_comp70_Black_Rhisp),
                       save_95evs(ratings_out, out_comp70_Latino_Rhisp),
                       save_95evs(ratings_out, out_comp80_White_Rhisp),
                       save_95evs(ratings_out, out_comp80_Asian_Rhisp),
                       save_95evs(ratings_out, out_comp80_Black_Rhisp),
                       save_95evs(ratings_out, out_comp80_Latino_Rhisp),
                       save_95evs(ratings_out, out_comp90_White_Rhisp),
                       save_95evs(ratings_out, out_comp90_Asian_Rhisp),
                       save_95evs(ratings_out, out_comp90_Black_Rhisp),
                       save_95evs(ratings_out, out_comp90_Latino_Rhisp),
                       save_95evs(ratings_out, out_comp50_White_Rasian),
                       save_95evs(ratings_out, out_comp50_Asian_Rasian),
                       save_95evs(ratings_out, out_comp50_Black_Rasian),
                       save_95evs(ratings_out, out_comp50_Latino_Rasian),
                       save_95evs(ratings_out, out_comp60_White_Rasian),
                       save_95evs(ratings_out, out_comp60_Asian_Rasian),
                       save_95evs(ratings_out, out_comp60_Black_Rasian),
                       save_95evs(ratings_out, out_comp60_Latino_Rasian),
                       save_95evs(ratings_out, out_comp70_White_Rasian),
                       save_95evs(ratings_out, out_comp70_Asian_Rasian),
                       save_95evs(ratings_out, out_comp70_Black_Rasian),
                       save_95evs(ratings_out, out_comp70_Latino_Rasian),
                       save_95evs(ratings_out, out_comp80_White_Rasian),
                       save_95evs(ratings_out, out_comp80_Asian_Rasian),
                       save_95evs(ratings_out, out_comp80_Black_Rasian),
                       save_95evs(ratings_out, out_comp80_Latino_Rasian),
                       save_95evs(ratings_out, out_comp90_White_Rasian),
                       save_95evs(ratings_out, out_comp90_Asian_Rasian),
                       save_95evs(ratings_out, out_comp90_Black_Rasian),
                       save_95evs(ratings_out, out_comp90_Latino_Rasian)
                       ))
# rename col names
colnames(ratings_evs) <- c("Mean", "Lower", "Upper")
# append covariate labels
ratings_evs <- ratings_evs %>% mutate(Composition = rep(c(rep("50/48/2", 4),
                                                    rep("60/38/2", 4),
                                                    rep("70/28/2", 4),
                                                    rep("80/18/2", 4),
                                                    rep("90/8/2", 4)), 4),
                       LargestGroup = rep(c("Whites", "Asians", "Blacks", "Latinos"), 20),
                       Rrace = c(rep("White Respondents", 20), 
                                 rep("Black Respondents", 20), 
                                 rep("Latino Respondents", 20),
                                 rep("Asian Respondents", 20)))

ratings_evs$Rrace <- as.factor(ratings_evs$Rrace)
ratings_evs$Rrace <- fct_relevel(ratings_evs$Rrace,
                                 c("White Respondents",
                                   "Latino Respondents",
                                   "Asian Respondents",
                                   "Black Respondents"))

ratings_evs$LargestGroup <- as.factor(ratings_evs$LargestGroup)
ratings_evs$LargestGroup <- fct_relevel(ratings_evs$LargestGroup,
                                 c("Whites",
                                   "Latinos",
                                   "Asians",
                                   "Blacks"))
```


```{r, fig.align= "center", fig.height = 4.33071, fig.width= 4.33071, echo = F, warning = F, message = F}
# plot
fig2 <- ggplot(ratings_evs,
       aes(x = fct_rev(Composition), y = Mean, group = LargestGroup)) +
  geom_point(aes(color = LargestGroup), 
             stat = "summary", fun.y = sum,
             size = 1,
             position = position_dodge(-0.3)) +
  geom_errorbar(aes(ymin = Lower, ymax = Upper, 
                    color = LargestGroup),
                width = 0.2,
                position = position_dodge(-0.3)) +
  stat_summary(fun.y = sum, geom = "line",
               aes(color = LargestGroup),
               position = position_dodge(-0.3)) +
  theme_bw() +
  ylim(2, 5) +
  facet_wrap(~Rrace, ncol = 2) +
  labs(y = "Perceived Diversity", x = "Neighborhood Distribution \n (Heterogeneity \U2192)",
       color = "Largest Group") +
  theme(legend.position="bottom", 
        axis.text=element_text(size=6),
        axis.title=element_text(size=7),
        strip.text = element_text(size = 6, 
                                  margin = margin(0.05,0,0.05,0, "cm")),
        legend.text=element_text(size=6),
        legend.title=element_text(size=7),
        legend.margin = margin(t = -0.1, unit='cm'),
        legend.key.height = unit(0.2, "cm")) 


fig2

ggsave("fig2.pdf", fig2, width = 11, height = 11, units = "cm", dpi = 600, device=cairo_pdf)

```

\newpage

# Moderation by Attitudes (Whites Only)

 **Composition is also held constant in these figures at the median value of "70/28/2"** 

```{r, echo = F, message = F, warning = F}
# create whites only datasets
whites <- filter(data, race == "White")

# Specify models
aa_model6B <- zelig(rating~ composition + aa_factor*largest_group +
                       age + gender + educ + inc_num + born,
                     id = "ResponseID",
                     model = "normal.gee",
                     data = whites,
                     cite = F)

immig_model7B <- zelig(rating~ composition + immigration_factor*largest_group +
                       age + gender + educ + inc_num + born,
                     id = "ResponseID",
                     model = "normal.gee",
                     data = whites,
                     cite = F)

# Set inputs (8 combinations for each model? 2 attitudes x 4 largest group. Assuming a constant composition value)
out_aa_favor_black <- setx(aa_model6B,
                             composition = "70/28/2",
                             aa_factor = "Strongly favor",
                             largest_group = "Black",
                             age = 51 ,
                             gender = "Female",
                             educ = "Some college",
                             inc_num = 75, 
                             born = "United States")
out_aa_favor_white <- setx(aa_model6B,
                             composition = "70/28/2",
                             aa_factor = "Strongly favor",
                             largest_group = "White",
                             age = 51 ,
                             gender = "Female",
                             educ = "Some college",
                             inc_num = 75, 
                             born = "United States")
out_aa_favor_hisp <- setx(aa_model6B,
                             composition = "70/28/2",
                             aa_factor = "Strongly favor",
                             largest_group = "Latino",
                             age = 51 ,
                             gender = "Female",
                             educ = "Some college",
                             inc_num = 75, 
                             born = "United States")
out_aa_favor_asian <- setx(aa_model6B,
                             composition = "70/28/2",
                             aa_factor = "Strongly favor",
                             largest_group = "Asian",
                             age = 51 ,
                             gender = "Female",
                             educ = "Some college",
                             inc_num = 75, 
                             born = "United States")
out_aa_oppose_black <- setx(aa_model6B,
                             composition = "70/28/2",
                             aa_factor = "Strongly oppose",
                             largest_group = "Black",
                             age = 51 ,
                             gender = "Female",
                             educ = "Some college",
                             inc_num = 75, 
                             born = "United States")
out_aa_oppose_white <- setx(aa_model6B,
                             composition = "70/28/2",
                             aa_factor = "Strongly oppose",
                             largest_group = "White",
                             age = 51 ,
                             gender = "Female",
                             educ = "Some college",
                             inc_num = 75, 
                             born = "United States")
out_aa_oppose_hisp <- setx(aa_model6B,
                             composition = "70/28/2",
                             aa_factor = "Strongly oppose",
                             largest_group = "Latino",
                             age = 51 ,
                             gender = "Female",
                             educ = "Some college",
                             inc_num = 75, 
                             born = "United States")
out_aa_oppose_asian <- setx(aa_model6B,
                             composition = "70/28/2",
                             aa_factor = "Strongly oppose",
                             largest_group = "Asian",
                             age = 51 ,
                             gender = "Female",
                             educ = "Some college",
                             inc_num = 75, 
                             born = "United States")

out_immig_increased_black <- setx(immig_model7B,
                             composition = "70/28/2",
                             immigration_factor = "Increased",
                             largest_group = "Black",
                             age = 51 ,
                             gender = "Female",
                             educ = "Some college",
                             inc_num = 75, 
                             born = "United States")
out_immig_increased_white <- setx(immig_model7B,
                             composition = "70/28/2",
                             immigration_factor = "Increased",
                             largest_group = "White",
                             age = 51 ,
                             gender = "Female",
                             educ = "Some college",
                             inc_num = 75, 
                             born = "United States")
out_immig_increased_hisp <- setx(immig_model7B,
                             composition = "70/28/2",
                             immigration_factor = "Increased",
                             largest_group = "Latino",
                             age = 51 ,
                             gender = "Female",
                             educ = "Some college",
                             inc_num = 75, 
                             born = "United States")
out_immig_increased_asian <- setx(immig_model7B,
                             composition = "70/28/2",
                             immigration_factor = "Increased",
                             largest_group = "Asian",
                             age = 51 ,
                             gender = "Female",
                             educ = "Some college",
                             inc_num = 75, 
                             born = "United States")
out_immig_decreased_black <- setx(immig_model7B,
                             composition = "70/28/2",
                             immigration_factor = "Decreased",
                             largest_group = "Black",
                             age = 51 ,
                             gender = "Female",
                             educ = "Some college",
                             inc_num = 75, 
                             born = "United States")
out_immig_decreased_white <- setx(immig_model7B,
                             composition = "70/28/2",
                             immigration_factor = "Decreased",
                             largest_group = "White",
                             age = 51 ,
                             gender = "Female",
                             educ = "Some college",
                             inc_num = 75, 
                             born = "United States")
out_immig_decreased_hisp <- setx(immig_model7B,
                             composition = "70/28/2",
                             immigration_factor = "Decreased",
                             largest_group = "Latino",
                             age = 51 ,
                             gender = "Female",
                             educ = "Some college",
                             inc_num = 75, 
                             born = "United States")
out_immig_decreased_asian <- setx(immig_model7B,
                             composition = "70/28/2",
                             immigration_factor = "Decreased",
                             largest_group = "Asian",
                             age = 51 ,
                             gender = "Female",
                             educ = "Some college",
                             inc_num = 75, 
                             born = "United States")

# run function on all 8 combos, save as dataframe
# Affirmative Action
aa_ratings_evs <- as.data.frame(rbind(
                       save_95evs(aa_model6B, out_aa_favor_black),
                       save_95evs(aa_model6B, out_aa_favor_white),
                       save_95evs(aa_model6B, out_aa_favor_hisp),
                       save_95evs(aa_model6B, out_aa_favor_asian),
                       save_95evs(aa_model6B, out_aa_oppose_black),
                       save_95evs(aa_model6B, out_aa_oppose_white),
                       save_95evs(aa_model6B, out_aa_oppose_hisp),
                       save_95evs(aa_model6B, out_aa_oppose_asian)
                       ))
# rename col names
colnames(aa_ratings_evs) <- c("Mean", "Lower", "Upper")
# append covariate labels
aa_ratings_evs <- aa_ratings_evs %>% mutate(LargestGroup = as.factor(rep(c("Black", 
                                                                           "White", 
                                                                           "Latino",
                                                                           "Asian"), 2)),
                         AffirmativeAction = as.factor(c(rep("Strongly favor", 4), 
                                               rep("Strongly oppose", 4))))
# make black the reference group
aa_ratings_evs$LargestGroup <- fct_relevel(aa_ratings_evs$LargestGroup,
                                           c("White", "Latino", "Asian", "Black"))

# IMMIGRATION
immig_ratings_evs <- as.data.frame(rbind(
                       save_95evs(immig_model7B, out_immig_increased_black),
                       save_95evs(immig_model7B, out_immig_increased_white),
                       save_95evs(immig_model7B, out_immig_increased_hisp),
                       save_95evs(immig_model7B, out_immig_increased_asian),
                       save_95evs(immig_model7B, out_immig_decreased_black),
                       save_95evs(immig_model7B, out_immig_decreased_white),
                       save_95evs(immig_model7B, out_immig_decreased_hisp),
                       save_95evs(immig_model7B, out_immig_decreased_asian)
                       ))
# rename col names
colnames(immig_ratings_evs) <- c("Mean", "Lower", "Upper")
# append covariate labels
immig_ratings_evs <- immig_ratings_evs %>% mutate(LargestGroup = as.factor(rep(c("Blacks", 
                                                                 "Whites", 
                                                                 "Latinos",
                                                                 "Asians"), 2)),
                         Immigration = as.factor(c(rep("Should be Increased", 4), 
                                               rep("Should be Decreased", 4))))
# relevel factors
immig_ratings_evs$LargestGroup <- fct_relevel(immig_ratings_evs$LargestGroup,
                                              c("Whites", "Latinos", "Asians", "Blacks"))
immig_ratings_evs$Immigration <- fct_relevel(immig_ratings_evs$Immigration,
                                             c("Should be Increased",
                                               "Should be Decreased"))

```


```{r, fig.align= "center", echo = F, warning = F, message = F, fig.width= 4.33071, fig.height= 4}
# aa 
fig3a <- ggplot(aa_ratings_evs,
       aes(x = LargestGroup, y = Mean, group = AffirmativeAction)) +
  geom_point(aes(color = AffirmativeAction), 
             stat = "summary",
             size = 1,
             fun.y = sum,
             position = position_dodge(0.3)) +
  geom_errorbar(aes(ymin = Lower, ymax = Upper, 
                    color = AffirmativeAction),
                width = 0.2,
                position = position_dodge(0.3)) +
  theme_bw() +
  ylim(2, 5) +
  labs(y = "Perceived Diversity", x = "Largest Group",
       color = "Affirmative Action") +
   theme(legend.position = c(.96, 0.04),
         legend.justification = c("right", "bottom"),
         legend.box.just = "right",
         legend.margin = margin(0.1, 0.1, 0.1, 0.1),
         legend.text=element_text(size=6),
         legend.title=element_text(size=7),
         legend.key.height = unit(0.2, "cm"),
         axis.text=element_text(size=6),
         axis.title=element_text(size=7)) 


# immigration
fig3b <- ggplot(immig_ratings_evs,
       aes(x = LargestGroup, y = Mean, group = Immigration)) +
  geom_point(aes(color = Immigration), 
             stat = "summary",
             size = 1,
             fun.y = sum,
             position = position_dodge(0.3)) +
  geom_errorbar(aes(ymin = Lower, ymax = Upper, 
                    color = Immigration),
                width = 0.2,
                position = position_dodge(0.3)) +
  theme_bw() +
  ylim(2, 5) +
  labs(x = "Largest Group", y = "",
       color = "Immigration") +
   theme(legend.position = c(.96, 0.04),
         legend.justification = c("right", "bottom"),
         legend.box.just = "right",
         legend.margin = margin(0.1, 0.1, 0.1, 0.1),
         legend.text=element_text(size=6),
         legend.title=element_text(size=7),
         legend.key.height = unit(0.2, "cm"),
         axis.text=element_text(size=6),
         axis.title=element_text(size=7)) 

fig3 <- fig3a + fig3b

fig3

ggsave("fig3.pdf", fig3, width = 11, height = 9, units = "cm", dpi = 600)
```

